package com.tyk.bookstore.front.cart.controller;


import com.tyk.bookstore.front.cart.model.query.CartAddQuery;
import com.tyk.bookstore.front.cart.model.query.CartPageQuery;
import com.tyk.bookstore.front.cart.model.query.CartUpdateQuery;
import com.tyk.bookstore.front.cart.model.vo.CartPageVo;
import com.tyk.bookstore.front.cart.model.vo.ShoppingCartVo;
import com.tyk.bookstore.front.cart.service.CartService;
import com.tyk.bookstore.front.common.core.model.result.R;
import com.tyk.bookstore.front.common.core.model.vo.PageVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author tyk
 * @description 购物车管理
 */
@RequestMapping({"api/cart"})
@Tag(name = "购物车管理", description = "购物车管理")
@RestController
@RequiredArgsConstructor
public class CartController {

    private final CartService cartService;


    @PostMapping
    @Operation(summary = "添加购物车", description = "添加购物车")
    public R<Object> add(@Validated @RequestBody CartAddQuery query) {
        cartService.add(query);
        return R.ok();
    }

    @GetMapping({"{bookId}"})
    @Deprecated
    @Operation(summary = "获取购物车详情", description = "获取购物车详情")
    public R<ShoppingCartVo> getByBookId(
            @Schema(description = "书籍ID") @NotNull @Min(1)
            @PathVariable Long bookId
    ) {
        return R.ok(cartService.getByBookId(bookId));
    }

    @GetMapping({"page"})
    @Operation(summary = "分页查询购物车", description = "分页查询购物车")
    public R<CartPageVo> getPage(
            @Validated @ParameterObject CartPageQuery query,
            BindingResult br
    ) {
        return br.hasErrors() ? R.ok(PageVo.instance(CartPageVo.class)) : R.ok(cartService.getPage(query));
    }

    @GetMapping({"list"})
    @Operation(summary = "查询购物车列表", description = "查询购物车列表")
    public R<List<ShoppingCartVo>> getList() {
        return R.ok(cartService.getList());
    }

    @PutMapping
    @Operation(summary = "更新购物车", description = "更新购物车")
    public R<Object> update(@Validated @RequestBody CartUpdateQuery query) {
        cartService.update(query);
        return R.ok();
    }

    @DeleteMapping({"{bookId}"})
    @Operation(summary = "删除购物车", description = "删除购物车")
    public R<Object> deleteByBookId(
            @Schema(description = "书籍ID") @NotNull @Min(1)
            @PathVariable Long bookId
    ) {
        cartService.deleteByBookId(bookId);
        return R.ok();
    }

    @DeleteMapping
    @Operation(summary = "批量删除购物车", description = "批量删除购物车")
    public R<Object> deleteBatchByBookIdSet(
            @Schema(description = "书籍ID列表") @NotNull @NotEmpty
            @RequestParam("book_id_list") Set<Long> bookIdList
    ) {
        cartService.deleteBatchByBookIdSet(bookIdList);
        return R.ok();
    }


    @PutMapping("select")
    @Operation(summary = "全选/全不选", description = "全选/全不选")
    public R<Object> selectAllOrCancelAll(
            @Validated @RequestParam("isAllSelected") Boolean isAllSelected
    ) {
        cartService.selectAllOrCancelAll(isAllSelected);
        return R.ok();
    }

    @DeleteMapping("clean")
    @Operation(summary = "移除已下架商品", description = "移除已下架商品")
    public R<Object> cleanOffProduct() {
        cartService.cleanOffProduct();
        return R.ok();
    }
}
